import { Injectable } from '@angular/core';
import { HttpService } from './http.service';

interface Gugu {
  requestSent?: Boolean,
  data: any
}

@Injectable()
export class UserService {

  userInfo: Gugu = {requestSent: false, data:{}}
  agentInfo: Gugu = {requestSent:false, data:{}}
  bankInfo: Gugu = {requestSent:false, data:''}
  shareInfo: Gugu = {requestSent:false, data:{}}
  isAgent = false;

  constructor(private http: HttpService) {
  }

  async getUserInfo(compulsive? : string) {
    if(this.userInfo.requestSent) return;
    const {data} = await this.http.post({
      name: 'car.get.user.info'
    })
    this.userInfo.data = data;
    this.userInfo.requestSent = true;
    this.isAgent = data.role == 2;
    return data;
  }

  async getAgentInfo(compulsive? : string) {
    if(this.agentInfo.requestSent && !compulsive) return;
    const {data} = await this.http.post({
      name: 'car.get.agent.page'
    })
    this.agentInfo.data = data;
    this.agentInfo.requestSent = true;
  }

  getBankInfo(compulsive? : string) {
    if(this.bankInfo.requestSent && !compulsive) return;
    this.http.post({
        name: 'car.get.bank.card'
      }).then(r => {
        this.bankInfo.data = r.data;
        this.bankInfo.requestSent = true;
      })
  }

  async getShareInfo(userId){
    if(this.shareInfo.requestSent) return;
    const r = await this.http.post({
      name: 'car.get.ticket',
      user_id: userId
    })
    this.shareInfo.data = r.data;
    this.shareInfo.requestSent = true;
    return r;
  }



}
